import 'dart:convert';

import 'package:awesome_project/models/data_models.dart';
import 'package:flutter/services.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../models/export_models.dart';

part 'other_riverpod.g.dart';

@riverpod
Future<List<CityModel>> getCityList(GetCityListRef ref) async {
  final cityS = await rootBundle.loadString("assets/pca.json");
  final cityJson = json.decode(cityS) as List<dynamic>;
  return cityJson.map((e) => CityModel.fromJson(e)).toList();
}

@riverpod
class OrganizationList extends _$OrganizationList {
  final page = PageModel();

  Future<HasMoreListData<OrganizationModel>> _organizationList(
      {bool isRefresh = true}) async {
    late final HasMoreListData<OrganizationModel> listData;
    final data = await rootBundle.loadString("assets/test.json");
    final list2 = json.decode(data) as List<dynamic>;
    final list = list2.map((e) => OrganizationModel.fromJson(e)).toList();

    print("===$type=$category");
    if (isRefresh) {
      listData = HasMoreListData<OrganizationModel>(list,
          hasMore: list.length >= page.pageSize);
    } else {
      state.value?.list?.addAll(list);
      listData = HasMoreListData<OrganizationModel>(state.value?.list,
          hasMore: list.length >= page.pageSize);
    }
    return listData;
  }

  Future<void> loadMore() async {
    page.page = page.page + 1;
    state = await AsyncValue.guard(() async {
      return _organizationList(isRefresh: false);
    });
  }

  @override
  Future<HasMoreListData<OrganizationModel>> build(
      {int? type, String? category}) async {
    page.resetPage();
    return _organizationList();
  }
}
